package com.gis.visualization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gis.visualization.config.result.R;
import com.gis.visualization.entity.EventDesc;
import com.gis.visualization.entity.HistoryEvent;
import com.gis.visualization.entity.HistorySummary;
import com.gis.visualization.entity.vo.EventQueryVo;
import com.gis.visualization.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 历史事件表 前端控制器
 * </p>
 *
 * @author hqz
 * @since 2021-02-23
 */
@RestController
@RequestMapping("/visualization/historyevent")
//@CrossOrigin
@Api(tags = "历史事件管理")
public class HistoryEventController {

    @Autowired
    private HistoryEventService eventService;

    @Autowired
    private EventDescService eventDescService;

    @Autowired
    private HistorySummaryService summaryService;

    @Autowired
    private OssService ossService;

    @Autowired
    private VideoService videoService;


    @ApiOperation("添加历史事件")
    @PostMapping("addEventInfo")
    public R addEvent(@RequestBody HistoryEvent historyEvent) {
        eventService.save(historyEvent);
        return R.ok().data("eventId", historyEvent.getId()).message("历史事件基本信息添加成功");
    }

    @ApiOperation("获取历史事件基本信息")
    @GetMapping("getEventInfo/{eventId}")
    public R getEventInfo(@PathVariable String eventId) {
        HistoryEvent historyEvent = eventService.getById(eventId);
        return R.ok().data("historyEvent", historyEvent).message("历史事件基本信息获取成功");
    }

    @ApiOperation("更新历史事件基本信息")
    @PostMapping("updateEventInfo")
    public R updateEventInfo(@RequestBody HistoryEvent historyEvent) {
        eventService.updateById(historyEvent);
        return R.ok().message("历史事件基本信息更新成功");
    }

    @ApiOperation("保存或者更新事件经过的描述")
    @PostMapping("saveOrUpdateDesc")
    public R saveOrUpdateDesc(@RequestBody EventDesc eventDesc) {
        QueryWrapper<EventDesc> wrapper = new QueryWrapper<>();
        wrapper.eq("id", eventDesc.getId());
        int count = eventDescService.count(wrapper);
        if (count > 0) {
            eventDescService.updateById(eventDesc);
            return R.ok().message("历史事件描述更新成功");
        } else {
            eventDescService.save(eventDesc);
            return R.ok().message("历史事件描述添加成功");
        }
    }

    @ApiOperation("获取事件的描述")
    @GetMapping("getDescInfo/{eventId}")
    public R getDescInfo(@PathVariable String eventId) {
        EventDesc eventDesc = eventDescService.getById(eventId);
        Map<String, Object> map = new HashMap<>();
        if (eventDesc == null) {
            map.put("desc", "");
            map.put("forms", new ArrayList<>());
        } else {
            map.put("desc", eventDesc.getDescription());
            String[] forms = eventDesc.getTypes().split(",");
            map.put("forms", forms);
        }
        return R.ok().data(map);
    }


    @ApiOperation("保存或者更新事件经过的描述")
    @PostMapping("saveOrUpdateSummary")
    public R saveOrUpdateSummary(@RequestBody HistorySummary summary) {
        QueryWrapper<HistorySummary> wrapper = new QueryWrapper<>();
        wrapper.eq("id", summary.getId());
        int count = summaryService.count(wrapper);
        if (count > 0) {
            summaryService.updateById(summary);
            return R.ok().message("历史事件总结更新成功");
        } else {
            summaryService.save(summary);
            return R.ok().message("历史事件总结添加成功");
        }
    }

    @ApiOperation("获取事件的描述")
    @GetMapping("getSummaryInfo/{summaryId}")
    public R getSummaryInfo(@PathVariable String summaryId) {
        HistorySummary historySummary = summaryService.getById(summaryId);
        if (historySummary == null) {
            return R.ok().data("summary", new HistorySummary());
        } else {
            return R.ok().data("summary", historySummary);
        }
    }

    //    条件查询带分页的方法
    @ApiOperation(value = "条件查询带分页的方法")
    @PostMapping("pageEventCondition/{current}/{limit}")
    public R pageEventCondition(@ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
                                @ApiParam(name = "limit", value = "单页数据个数", required = true) @PathVariable long limit,
                                @ApiParam(name = "EventQuery", value = "查询条件对象", required = false) @RequestBody(required = false) EventQueryVo eventQueryVo) {

        Map<String, Object> map = eventService.pageEventCondition(current, limit, eventQueryVo);
        return R.ok().data(map);
    }


    @ApiOperation("根据id删除事件")
    @DeleteMapping("deleteEvent/{eventId}")
    public R deleteEvent(@PathVariable String eventId) {
        eventService.deleteEvent(eventId);
        return R.ok().message("删除成功");
    }

    @ApiOperation("从excel中读取事件进行存储")
    @PostMapping("eventFromExcel")
    public R eventFromExcel(MultipartFile file) {
        if(file == null){
            return R.error().message("文件为空，请选择excel文件");
        }
        eventService.eventFromExcel(file,eventService,
                eventDescService,summaryService,
                ossService,videoService);
        return R.ok().message("成功从excel中读取历史事件存储到数据库中");
    }
}

